import {defineStore} from 'pinia'
import {ref} from 'vue'
import { userInfoService } from '@/api/user'

const useUserInfoStore = defineStore('userInfo',()=>{
    //定义状态相关的内容
    const info = ref({})

    const setInfo = (newInfo)=>{
        info.value = newInfo
    }

    const removeInfo = ()=>{
        info.value = {}
    }

    const getUserInfo = async () => {
        try {
            const res = await userInfoService()
            if (res.code === 0 && res.data) {
                setInfo(res.data)
                return res.data
            }
            return null
        } catch (error) {
            console.error('获取用户信息失败:', error)
            return null
        }
    }

    return {
        info,
        setInfo,
        removeInfo,
        getUserInfo
    }
},{persist:true})

export default useUserInfoStore